package ysf.com.mediumdifficulty;

import ysf.com.listnode.ListNode;

/**
 * 二十四  给定一个链表然后两两交换位置
 * @author ysf
 * @date 2021/7/31 13:57
 */
public class TwentyFour {


    public static void main(String[] args) {
        ListNode ls = new ListNode(1);
        ls.add(2);
        ls.add(3);
        ls.add(4);
        ls.add(5);
        ls.print();
        ListNode swaps = swaps(ls);
        swaps.print();

    }

    /**
     * 迭代法
     * @param head
     * @return
     */
    private static ListNode swaps(ListNode head){
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode point = dummy;
        while (point.next !=null && point.next.next != null){
            ListNode swap1 = point.next;
            swap1.print();
            ListNode swap2 = point.next.next;
            swap2.print();
            point.next = swap2;
            swap1.next = swap2.next;
            swap2.next = swap1;
            point = swap1;
        }
        return dummy.next;
    }





}
